package xyz.msj.hrm._01_hello;

import com.rabbitmq.client.*;
import xyz.msj.hrm.util.ConnectionUtil;

import java.io.IOException;

/**
 * 消息消费者
 */
public class receive {

    public static void main(String[] args) throws Exception {
        System.out.println("接收到消息");


        //1、获取连接

        Connection connection = ConnectionUtil.getConnection();
//        2、创建管道
        Channel channel = connection.createChannel();



        //3、创建消费者回调对象
        Consumer callBack = new DefaultConsumer(channel) {



            /**
             * No-op implementation of {@link Consumer#handleDelivery}.
             *
             * @param consumerTag
             * @param envelope
             * @param properties
             * @param body
             */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {




                System.out.println("消费者标签"+consumerTag);
                System.out.println("获取交换机"+envelope.getExchange());
                System.out.println("获取路由队列名"+envelope.getRoutingKey());
                System.out.println("获取队列id："+envelope.getDeliveryTag());
                System.out.println("获取对象信息" + new String(body));

                //手动签收设置
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        };


        // int i = 1 / 0;

//        4、消费消息
//        String queue, :队列名
//        boolean autoAck, 自动确认
//        Consumer callback 回调
        /**
         * 监听队列String queue, boolean autoAck,Consumer callback
         * 参数明细
         * 1、队列名称
         * 2、是否自动回复，设置为true为表示消息接收到自动向mq回复接收到了，mq接收到回复会删除消息，设置
         为false则需要手动回复
         * 3、消费消息的方法，消费者接收到消息后调用此方法
         */

        channel.basicConsume(Sender.RABBIT_QUEUE_HELLO, false, callBack);
        System.out.println("消息处理完毕！！！");
    }
}
